package com.sc.nft.dao;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sc.nft.entity.EcologyPoolDaoRank;
import com.sc.nft.entity.vo.ExcellentBrandRankingVO;
import com.sc.nft.mapper.EcologyPoolDaoRankMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * 生态池-品牌榜(sc_ecology_pool_dao_rank)数据DAO
 *
 * @author jinpinhai
 * @description Generator
 * @since 2024-07-17 11:00:11
 */
@Slf4j
@Repository
public class EcologyPoolDaoRankDao extends ServiceImpl<EcologyPoolDaoRankMapper, EcologyPoolDaoRank> {

    public Page<EcologyPoolDaoRank> pageDaoRank(int pageNo, int pageSize, Long rankConfId, EcologyPoolDaoRank lastDate) {
        LambdaQueryWrapper<EcologyPoolDaoRank> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(EcologyPoolDaoRank::getEcologyPoolDaoRankConfId, rankConfId);
        wrapper.eq(EcologyPoolDaoRank::getRankDate, lastDate.getRankDate());
        wrapper.eq(EcologyPoolDaoRank::getShowStatus, Boolean.TRUE);
        wrapper.orderByAsc(EcologyPoolDaoRank::getSort);
        return page(new Page<>(pageNo, pageSize), wrapper);
    }

    public List<EcologyPoolDaoRank> listByRankConfId(Long rankConfId, EcologyPoolDaoRank lastDate) {
        LambdaQueryWrapper<EcologyPoolDaoRank> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(EcologyPoolDaoRank::getEcologyPoolDaoRankConfId, rankConfId);
        wrapper.eq(EcologyPoolDaoRank::getRankDate, lastDate.getRankDate());
        wrapper.eq(EcologyPoolDaoRank::getShowStatus, Boolean.TRUE);
        wrapper.orderByAsc(EcologyPoolDaoRank::getSort);
        return list(wrapper);
    }

    /**
     * 获取榜单最大日期
     *
     * @param confId
     * @return
     */
    public EcologyPoolDaoRank getMaxRankDate(Long confId) {
        LambdaQueryWrapper<EcologyPoolDaoRank> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(EcologyPoolDaoRank::getEcologyPoolDaoRankConfId, confId);
        wrapper.orderByDesc(EcologyPoolDaoRank::getRankDate);
        wrapper.last("limit 1");
        return baseMapper.selectOne(wrapper);
    }

    public Page<EcologyPoolDaoRank> showPage(Page<EcologyPoolDaoRank> page, Long rankConfId, Date rankDate) {
        LambdaQueryWrapper<EcologyPoolDaoRank> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(EcologyPoolDaoRank::getEcologyPoolDaoRankConfId, rankConfId);
        wrapper.eq(EcologyPoolDaoRank::getRankDate, rankDate);
        wrapper.eq(EcologyPoolDaoRank::getShowStatus, Boolean.TRUE);
        wrapper.orderByAsc(EcologyPoolDaoRank::getSort);
        return page(page, wrapper);
    }

    /**
     * 批量入库国库数据
     *
     * @param batch
     */
    public void saveBatchExchequer(List<ExcellentBrandRankingVO> batch) {
        baseMapper.saveBatchExchequer(batch);
    }

    /**
     * 获取国库对应配置的Id
     *
     * @param configId
     * @param contributionThreshold
     * @return
     */
    public EcologyPoolDaoRank listByRankConfIdAndcContributionThreshold(Long configId, BigDecimal contributionThreshold, List<Long> userIdExcludeList, BigDecimal configContributionThreshold) {
        LambdaQueryWrapper<EcologyPoolDaoRank> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(EcologyPoolDaoRank::getEcologyPoolDaoRankConfId, configId);
        wrapper.ge(EcologyPoolDaoRank::getContribution, contributionThreshold);
        wrapper.ge(EcologyPoolDaoRank::getContribution, configContributionThreshold);
        wrapper.eq(EcologyPoolDaoRank::getShowStatus, Boolean.FALSE);
        wrapper.notIn(CollUtil.isNotEmpty(userIdExcludeList), EcologyPoolDaoRank::getDaoId, userIdExcludeList);
        wrapper.orderByDesc(EcologyPoolDaoRank::getContribution);
        wrapper.orderByAsc(EcologyPoolDaoRank::getDaoId);
        wrapper.last("limit 1");
        return getOne(wrapper);
    }

    public EcologyPoolDaoRank compareDaoRank(Long daoId, Long userId, DateTime dateTime) {
        LambdaQueryWrapper<EcologyPoolDaoRank> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(EcologyPoolDaoRank::getUserId, userId);
        wrapper.eq(EcologyPoolDaoRank::getDaoId, daoId);
        // 把时间dateTime转换格式为YYYY-MM-DD
        wrapper.eq(EcologyPoolDaoRank::getRankDate, dateTime.toString("yyyy-MM-dd"));
        wrapper.eq(EcologyPoolDaoRank::getShowStatus, Boolean.TRUE);
        wrapper.orderByDesc(EcologyPoolDaoRank::getId);
        wrapper.last("limit 1");
        return getOne(wrapper);
    }
}